<template>
  <div style="margin: 20px;">
    <el-row :gutter="20">
      <el-col :span="12" :offset="6">
        <el-form :label-position="labelPosition" label-width="80px" :model="user">
          <el-form-item label="用户名">
            <el-input v-model="user.username"></el-input>
          </el-form-item>
<!--          <el-form-item label="手机号">-->
<!--            <el-input v-model="user.tel"></el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="性别">-->
<!--            <el-input v-model="user.type"></el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="血型">-->
<!--            <el-input v-model="user.username"></el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="生日">-->
<!--            <el-input v-model="user.name"></el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="居住地">-->
<!--            <el-input v-model="formLabelAlign.name"></el-input>-->
<!--          </el-form-item>-->
<!--          <el-form-item label="生日">-->
<!--            <el-input v-model="formLabelAlign.name"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="邮箱">
            <el-input v-model="user.useremail"></el-input>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              action="api/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl" width="100px;height:100px" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm()">确认修改</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "changeinfo",
  data() {
    return {
      user:{
        username:'',
        useremail:'',
        userimage:''
      },
      labelPosition: 'right',
      formLabelAlign: {
        name: '',
        region: '',
        type: '',
        imageUrl:''
      }
    };
  },
  methods:{
    submitForm(){
      this.$axios.post("/api/user/update",this.user).then(res=>{
        if (res.data.code==1){
          alert("修改成功")
          this.$router.push("/userinfoindex")
        }else{
          alert("信息输入有误,请重新输入")
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      // console.log(res)
      this.user.userimage=res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>

<style scoped>

</style>
